package com.leonzhangxf.spel.reference;

import org.springframework.expression.spel.SpelParserConfiguration;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.SimpleEvaluationContext;

import java.util.Map;

/**
 * Inline Maps
 * <p>
 * {:} by itself means an empty map. For performance reasons,
 * if the map is itself composed of fixed literals or other nested constant structures (lists or maps),
 * a constant map is created to represent the expression (rather than building a new map on each evaluation).
 * <p>
 * Quoting of the map keys is optional. The examples below do not use quoted keys.
 *
 * @author leonzhangxf
 * @date 20190427
 */
public class InlineMaps {

    public static void main(String[] args) {
        SpelParserConfiguration parserConfiguration =
            new SpelParserConfiguration(true, true);
        SpelExpressionParser parser = new SpelExpressionParser(parserConfiguration);
        SimpleEvaluationContext context = SimpleEvaluationContext.forReadWriteDataBinding().build();

        Map inventorInfo = parser.parseExpression("{name:'Nikola',dob:'10-July-1856'}")
            .getValue(context, Map.class);
        System.out.println(inventorInfo);

        // evaluates to a Java map containing the two entries
        Map mapOfMaps = parser
            .parseExpression("{name:{first:'Nikola',last:'Tesla'},dob:{day:10,month:'July',year:1856}}")
            .getValue(context, Map.class);
        System.out.println(mapOfMaps);
    }
}
